#include<iostream>
#include<string>
#include<unistd.h>
#include"Thread.hpp"
#include"lockGuard.hpp"
using namespace std;
int tickets=10000;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
// class TData
// {
// public:
//     TData(const string &name,pthread_mutex_t *mutex):_name(name),_pmutex(mutex)
//     {}
//     ~TData()
//     {}
// public:
//     string _name;
//     // pthread_mutex_t*_pmutex;
// };
void threadRoutine(void*args)
{
    std::string message=static_cast<const char*>(args);
    while(true)
    {
        // pthread_mutex_lock(td->_pmutex);
        if(tickets>0)
        {
            usleep(2000);
            cout<<message<<"get a ticket:"<<tickets--<<endl;
            // pthread_mutex_unl ock(td->_pmutex);

        }
        else
        {
            // pthread_mutex_unlock(td->_pmutex);
            break;
        }
        // usleep(100);
    }
    // return nullptr;
}
void threadRun(void*args)
{
    std::string message=static_cast<const char*>(args);
    int cnt=10;
    while(cnt)
    {
        cout<<"我是一个线程,"<<message<<" cnt:"<<cnt--<<endl;
        sleep(1);
    }
}
int main()
{
    Thread t1(1,threadRoutine,(void*)"helloxiaolu1");
    Thread t2(2,threadRoutine,(void*)"helloxiaolu2");
    Thread t3(3,threadRoutine,(void*)"helloxiaolu3");
    Thread t4(4,threadRoutine,(void*)"helloxiaolu4");
    t1.run();
    t2.run();
    t3.run();
    t4.run();
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    // Thread t1(1,threadRoutine,);
    // cout<<"thread name:"<<t1.threadname()<<" thread id:"<<t1.threadid()<<" thread status :"<<t1.status()<<endl;
    // t1.run();
    // // sleep(100);
    // cout<<"thread name:"<<t1.threadname()<<" thread id:"<<t1.threadid()<<" thread status :"<<t1.status()<<endl;
    // t1.join();
    // cout<<"thread name:"<<t1.threadname()<<" thread id:"<<t1.threadid()<<" thread status :"<<t1.status()<<endl;

    return 0;
}
// int main()
// {
//     pthread_mutex_t mutex;
//     pthread_mutex_init(&mutex,nullptr);
//     pthread_t tids[4];
//     int n=sizeof(tids)/sizeof(tids[0]);
//     for(int i=0;i<n;i++)
//     {
//         char*name;
//         snprintf(name,64,"thread-%d",i+1);
//         TData*td=new TData(name,&mutex);
//         pthread_create(tids+i,nullptr,threadRoutine,td);
//     }
//     for(int i=0;i<n;i++)
//     {
//         pthread_join(tids[i],nullptr);
//     }
//     pthread_mutex_destroy(&mutex);
//     return 0;
// }